<?php


namespace app\common\service\rabbitMQ;


use app\common\exception\LogicException;
use PhpAmqpLib\Exchange\AMQPExchangeType;
use PhpAmqpLib\Message\AMQPMessage;

class OrderCreated implements RabbitMQBusinessInterface
{
    public static function instance($expire_timestamp = 0)
    {
        $rabbitMQ = (new RabbitMQ());
        $rabbitMQ->setDurable(true);
        $rabbitMQ->setExchange(__CLASS__ . '_direct');
        $rabbitMQ->setExchangeType(AMQPExchangeType::DIRECT);
        $rabbitMQ->setRoutingKey(__CLASS__);
        $rabbitMQ->setQueue(__CLASS__);
        $rabbitMQ->setConfirmSelectType(
            3,
            [
                static function (AMQPMessage $message) {
                },
                static function (AMQPMessage $message) {
                    throw new LogicException();
                },
                static function ($replyCode, $replyText, $exchange, $routingKey, AMQPMessage $message) {
                },
            ]
        );
        $rabbitMQ->prepare();
        $rabbitMQ->preparePublisher();
        return $rabbitMQ;
    }

    public static function publisher(array $data, int $expire_timestamp = 0): void
    {
        $rabbitMQ = self::instance($expire_timestamp);
        $rabbitMQ->publisher(
            [
                [
                    'data' => $data,
                ],
            ]
        );
    }

    public static function consumer($data)
    {
        return false;
    }
}